﻿/*
*  ChgDoc CMS - GNU General Public License, version 3 (GPL-3.0)
*  Copyright (C)  2012. Ian Quigley
*  
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by 
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*  
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*/



selectFileDialog = function (options) {

    var internal = {
        selected: function (file) { }
    };

    if (options !== undefined)
        for (var member in options)
            internal[member] = options[member];


    populateList = function (selector, dir) {

        var jqElement = $(selector);

        jqElement.html("<p style='text-align:center'>Loading</p>");

        $.ajax({
            url: appGlobal.serverUrl + 'service/files.asmx/DirectoryList',
            type: "POST",
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{\"from\" : \"" + dir + "\"}",
            dataType: "json",
            success: function (msg) {
                var obj = $.parseJSON(msg.d);
                displayFileList(selector, obj.content, obj.rootUrl, dir);
            }
        });
    }

    displayFileList = function (selector, list, rootUrl, dir) {

        var html

        if (dir.length > 0) {
            var back = dir.substr(0, dir.lastIndexOf('/'));
            html = "<h4><a href=\"#\" onclick=\"populateList('" + selector + "','" + back + "')\" style='float:right; margin-right:10px;'>Back</a>" + dir + "</h4>";
        }
        else {
            html = '<h4>[root]</h4>';
        }

        for (var i = 0; i < list.length; i++) {

            if (list[i].type === 'directory')
                html += "<div style='font-size: 12px; padding: 3px; overflow:hidden' onclick=\"populateList('" + selector + "', '" + dir + '/' + list[i].value + "')\">" + list[i].value + "</div>";
            else
                html += "<div style='font-size: 12px; padding: 3px; overflow:hidden' onclick=\"selected('" + rootUrl + dir + '/' + list[i].value + "')\" >" + list[i].value + "</div>";
        }

        $(selector).html(html);
    };

    selected = function (file) {
        var ret = internal.selected(file);
        if (typeof ret === undefined || ret === undefined || ret === null || ret === true)
            $('#folderList').dialog('close');
    };

    $('body').append("<div id='folderList'></div>");

    $('#folderList').dialog({
        title: "Select file",
        width: 600,
        height: 400,
        close: function () { $(this).dialog("destory"); $('#folderList').remove(); }
    });

    populateList('#folderList', "");


}
